package Tools;

import java.io.IOException;
import java.io.InputStream;

import com.fs.zise.ZiSeActivity;

import Appconst.GameConst;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;

public class BitmapTool{
	
	
	/** 装载一张图片 **/
	public static Bitmap loadBitmap(Context _context, int ID) {
		return BitmapFactory.decodeStream(_context.getResources()
				.openRawResource(ID), null, new BitmapFactory.Options());
	}

	public static Bitmap loadBitmap(int nameID) {
		Bitmap bitmap = null;
		bitmap = BitmapTool.getImageFromAssetFile("a" + nameID + ".png");
		// int resID=getResources().getIdentifier("a"+nameID,
		// "drawable",getContext().getPackageName());
		// if(resID!=0)
		// {
		// bitmap=BitmapFactory.decodeResource(getResources(), resID);
		// }
		return bitmap;
	}

	
	public static Bitmap getImageAndScaleFromAssetFile(String fileName,float scaleRatioX,float scaleRatioY)
	{
		Bitmap orgImage = null;
		try {
			AssetManager assetManager = ZiSeActivity.getActivity().getResources().getAssets();
			InputStream is = assetManager.open(fileName);
			orgImage = BitmapFactory.decodeStream(is);
			is.close();
		} catch (Exception e) {
			System.out.println("读取图片有误------");
		}

		int org_W = orgImage.getWidth();
		int org_H = orgImage.getHeight();
		
		Matrix matrix = new Matrix();
		matrix.postScale(scaleRatioX, scaleRatioY);
		
		Bitmap scaledImageBitmap = Bitmap.createBitmap(orgImage, 0, 0, org_W, org_H, matrix, true);
		orgImage = null;
		
		return scaledImageBitmap;
	}

	public static Bitmap createImage(Drawable tile, int w, int h) {
		Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		tile.setBounds(0, 0, w, h);
		tile.draw(canvas);
		return bitmap;
	}
	
	
	public static InputStream getAssetsFromFile(String fileName)
	{
			try {
				AssetManager assetManager = ZiSeActivity.getActivity().getResources().getAssets();
				InputStream is = assetManager.open(fileName);
				return is;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
	}
	
	/**
	 * 
	 * @param fileName assets图片名字
	 * @return bitmap 
	 */
	public static  Bitmap getImageFromAssetFile(String fileName)
	{
		Bitmap image = null;
		try {
			AssetManager assetManager = ZiSeActivity.getActivity().getResources().getAssets();
			InputStream is = assetManager.open(fileName);
			image = BitmapFactory.decodeStream(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("读取图片有误------");
		}
		return image;
	}
	
	public static void drawClipImage(Canvas canvas,Paint paint, Bitmap bitmap, int x, int y, int src_x, int src_y, int src_w, int src_h) {
		canvas.save();
	    canvas.clipRect(x, y, x + src_w, y + src_h);
	    canvas.drawBitmap(bitmap, x - src_x, y - src_y,paint);
	    canvas.restore();
	}
	
	
	public static void drawClipImageAndScale(Canvas canvas,Paint paint, Bitmap bitmap, int x, int y, int src_x, int src_y, int src_w, int src_h,boolean b_scale,float scalew,float scaleh) {
		
		
	}
	
	/**
	 * 
	 * @param canvas 画布
	 * @param paint  画笔
	 * @param bitmap 原始图片
	 * @param trans  翻转参数  0 不翻转  1 水平翻转 2 垂直翻转
	 * @param scrx   图片画到画布上的坐标x
	 * @param scry   图片画到画布上的坐标y
	 * @param resx   图片上切片原点x
	 * @param resy   图片上切片原点y
	 * @param w      图片上切片宽
	 * @param h      图片上切片高
	 */
	public static void drawRegoin(Canvas canvas,Paint paint,Bitmap bitmap,int trans,int scrx,int scry,int resx,int resy,int w,int h)
	{
		float transx = 1f;
		float transy = 1f;
		if(trans==1)
		{
			transx = -1f;
			transy =  1f;
			resx = bitmap.getWidth()-resx-w;
			
		}else if(trans==2){
			transx =  1f;
			transy = -1f;
			resy = bitmap.getHeight()-resy- h;
		}
		else if(trans==3){
			transx =  -1f;
			transy = -1f;
			resx = bitmap.getWidth()-resx-w;
			resy = bitmap.getHeight()-resy- h;
		}
		Matrix matrix = new Matrix();
		matrix.postScale(transx, transy, transx==-1f?bitmap.getWidth()/2:0, transy==-1f?bitmap.getHeight()/2:0);
		canvas.save();
		canvas.clipRect(scrx, scry, scrx+w, scry+h);
		matrix.postTranslate((float)scrx-resx, (float)scry-resy);
		canvas.drawBitmap(bitmap, matrix, paint);
		canvas.restore();
		canvas.clipRect(0, 0,GameConst.SCR_W, GameConst.SCR_H);
	}
	
	/**
	 * 
	 * @param canvas 画布
	 * @param paint  画笔
	 * @param bitmap 原始图片
	 * @param scalew  放大后的图片宽
	 * @param scaleh  放大后的图片高
	 * @param scrx    放大后图片画的坐标
	 * @param scry    放大后图片画的坐标
	 */
	public static void drawScale(Canvas canvas,Paint paint,Bitmap bitmap,float scalew,float scaleh,float scrx,float scry)
	{
		Matrix matrix = new Matrix();
		matrix.postScale(scalew/bitmap.getWidth(), scaleh/bitmap.getHeight());
		matrix.postTranslate(scrx, scry);
		canvas.drawBitmap(bitmap, matrix, paint);
	}
	public static void drawRotate(Canvas canvas,Paint paint,Bitmap bitmap,int x,int y,float angle)
	{
		Matrix matrix = new Matrix();
		matrix.setTranslate(x-bitmap.getWidth()/2, y-bitmap.getHeight()/2);     //设置图片的旋转中心，即绕（X,Y）这点进行中心旋转
		matrix.preRotate(angle, (float)bitmap.getWidth()/2, (float)bitmap.getHeight()/2);  //要旋转的角度
		canvas.drawBitmap(bitmap, matrix, null);

		
	}
	public static void drawRotate(Canvas canvas,Paint paint,Bitmap bitmap,float x,float y,float angle)
	{
		Matrix matrix = new Matrix();
		matrix.setTranslate(x-bitmap.getWidth()/2, y-bitmap.getHeight()/2);     //设置图片的旋转中心，即绕（X,Y）这点进行中心旋转
		matrix.preRotate(angle, (float)bitmap.getWidth()/2, (float)bitmap.getHeight()/2);  //要旋转的角度
		canvas.drawBitmap(bitmap, matrix, null);

		
	}
	 public void drawTriangle(Canvas canvas,Paint paint,int px1,int py1,int px2,int py2,int px3,int py3)
	{
		
	    	canvas.drawLine(px1, py1, px2, py2,paint);
	    	canvas.drawLine(px2, py2, px3, py3,paint);
	    	canvas.drawLine(px3, py3, px1, py1,paint);
	}
	    /**
		 * 近似求两点距离 精度较高
		 * @param x1 第一点X坐标
		 * @param y1 第一点Y坐标
		 * @param x2 第二点X坐标
		 * @param y2 第二点Y坐标
		 * @return 返回两点距离
		 */
		public static int distance2D( int x1, int y1,int x2,int y2 )
		{
			
			int dx = 0;
			int dy = 0;
			dx = x2 - x1;
			dy = y2 - y1;
			int min, max;
			if (dx < 0)
				dx = -dx;
			if (dy < 0)
				dy = -dy;

			if (dx < dy) {
				min = dx;
				max = dy;
			} else {
				min = dy;
				max = dx;
			}
			// coefficients equivalent to ( 123/128 * max ) and ( 51/128 * min )
			return (((max << 8) + (max << 3) - (max << 4) - (max << 1) + (min << 7)
					- (min << 5) + (min << 3) - (min << 1)) >> 8);
		}
			/**
			 * 画数字
			 * @param x y 画数字坐标
			 * @param num 数字
			 * @param image 数字图片
			 * @param showNumberTime -1为画无限时间 大于0为画循环单位时间
			 * */
			public static void drawNumber(Canvas canvas,Paint paint,int x,int y,int num,Bitmap[] image,int showNumberTime)
			{
				int[] temparr = {-1,-1,-1,-1,-1};
				int mark = -1;
				temparr[0] =  num/10000;
				temparr[1] = (num%10000)/1000;
				temparr[2] = (num%1000)/100;
				temparr[3] = (num%100)/10;
				temparr[4] =  num%10;
				for (int i = 0; i < temparr.length; i++) {
					if(temparr[i]>0)
					{
						mark = i;
						break;
					}
				}
				if(showNumberTime>0||showNumberTime == -1){
					if(mark>=0)
					for (int i = mark; i < temparr.length; i++) {						
//						g.drawImage(image[temparr[i]], x+(i-mark)*image[temparr[i]].getWidth(), y, 0);
						canvas.drawBitmap(image[temparr[i]], x+(i-mark)*image[temparr[i]].getWidth(), y,  paint);
					}
					else {
						canvas.drawBitmap(image[temparr[0]], x, y,  paint);
					}
				}
				
				
				if(showNumberTime>0)
				{
					showNumberTime --;
				}
				
			}
			//切图片
		public static Bitmap[] cellImage(Bitmap img,int cellW,int cellH)
				{		
					int t_ImgW = img.getWidth();
					int t_ImgH = img.getHeight();
					
					int row = t_ImgH / cellH;
					int col = t_ImgW / cellW;
					
					Bitmap[] o_Img = new Bitmap[row*col];
					for(int i=0;i<o_Img.length;i++)
					{
						o_Img[i] = Bitmap.createBitmap(img,cellW*(i%col),cellH*(i/col),cellW,cellH);
//							Image.createImage(img,cellW*(i%col),cellH*(i/col),cellW,cellH,0);
					}
					img = null;	
					return o_Img;
		}


}
